package cn.sinata.wanglin.village.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;

import com.xilada.xldutils.fragment.BaseLazyFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.adapter.NewsinfoPagerAdapter;
import cn.sinata.wanglin.adapter.SameCityPagerAdapter;
import cn.sinata.wanglin.model.SameCityClass;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

/**
 * description：小区---同城八卦
 * Created by kaihongwu on 2016/12/06 上午 9:45
 */
public class CitygossipFragment extends BaseLazyFragment {
    @BindView(R.id.viewpager_citygossip)
    ViewPager viewpager;//
    @BindView(R.id.tabs_citygossip)
    TabLayout tabs;//
    ArrayList<SameCityClass> mdatas=new  ArrayList<SameCityClass>();
    private SameCityPagerAdapter mPagerAdapter;
    @Override
    protected int getContentViewLayoutID() {

        return R.layout.fragment_citygossip;
    }

    @Override
    protected void onFirstVisibleToUser() {
        setupViewPager();
    }

    @Override
    protected void onVisibleToUser() {

    }

    @Override
    protected void onInvisibleToUser() {

    }
    private void setupViewPager() {
        if(mPagerAdapter==null){
            mPagerAdapter=new SameCityPagerAdapter(getChildFragmentManager(),mdatas);
            viewpager.setAdapter(mPagerAdapter);
            tabs.setupWithViewPager(viewpager);
            getClassfity();
        }

    }
    public void reFresh(){
        if(viewpager!=null){
            viewpager.setCurrentItem(0);
        }
        if (mPagerAdapter!=null){
            mPagerAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 获取到分类
     */
    private void getClassfity() {
        HttpManager.getClassify().subscribe(new ResultDataSubscriber<ArrayList<SameCityClass>>(CitygossipFragment.this) {
                                                @Override
                                                public void onSuccess(String msg, ArrayList<SameCityClass> sameCityClasses) {
                                                    if(sameCityClasses==null||sameCityClasses.size()==0){
                                                        return;
                                                    }
                                                    mdatas.clear();
                                                    mdatas.addAll(sameCityClasses);
                                                    mPagerAdapter.notifyDataSetChanged();
                                                }
                                            }
        );
    }
}
